package com.wanxi.servlet;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.*;
import com.wanxi.service.NewsService;
import com.wanxi.service.ProductService;
import com.wanxi.service.impl.NewsServiceImpl;
import com.wanxi.service.impl.ProductServiceImpl;
import com.wanxi.vo.NewsVO;
import com.wanxi.vo.ProductVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 将前端首页的数据发送到前端
 * 1.数据怎么发送给前端？json对象发送给前端
 */
@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的值

//        3.封装到model
//        新闻对象
        NewsVO newsVO = new NewsVO();
        newsVO.setWhereShow(1);
        newsVO.setPage(1);
        newsVO.setLimit(3);

//        产品对象
        ProductVO productVO = new ProductVO();
        productVO.setWhereShow(1);
        productVO.setPage(1);
        productVO.setLimit(2);

//        4.调用service
        NewsService newsService = new NewsServiceImpl();
        ProductService productService = new ProductServiceImpl();
//        5.得到service返回的值
        IndexDTO indexDTO = new IndexDTO();
        List<NewsDTO> newsDTOList = newsService.getData(newsVO);
        List<ProductDTO> productDTOList = productService.getData(productVO);
        indexDTO.setNewsDTOList(newsDTOList);
        indexDTO.setProductDTOList(productDTOList);
//        6.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(indexDTO));
    }
}
